﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebCalendar.Models;
using WebCalendar.Models.Authorization;

namespace WebCalendar.Controllers
{
    public class EntryAPIController : ApiController
    {
        private WebCalendarContext db = new WebCalendarContext();

        // GET api/EntryAPI
        public IEnumerable<Entry> GetEntries(int id)
        {
            var user = WebCalendarUser.GetUserProfile(db);
            var entries = user.CalendarRoles.FirstOrDefault(m=>m.Calendar.ID == id).Calendar.EntryFilters.Where(m=> m.Visible).Select(m=> m.Entry);
            
            return db.Entries.AsEnumerable();
        }

        // GET api/EntryAPI/5
        public Entry GetEntry(int id)
        {
            Entry entry = db.Entries.Find(id);
            if (entry == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return entry;
        }

        // PUT api/EntryAPI/5
        public HttpResponseMessage PutEntry(int id, Entry entry)
        {
            if (ModelState.IsValid && id == entry.ID)
            {
                db.Entry(entry).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/EntryAPI
        public HttpResponseMessage PostEntry(int id, Entry entry)
        {
            if (ModelState.IsValid)
            {
                
                var dbEntry = new Entry
                {
                    Title = entry.Title,
                    Description = entry.Description,
                    StartDate = entry.StartDate,
                    EndDate = entry.EndDate,
                    IsAllDay = entry.IsAllDay,
                    RecurringDays = entry.RecurringDays
                };
                //var pe = new PersonalEntry { EntryBase = dbEntry, Category = 
                //db.Entries.Add(dbEntry);

                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, entry);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entry.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/EntryAPI/5
        public HttpResponseMessage DeleteEntry(int id)
        {
            Entry entry = db.Entries.Find(id);
            if (entry == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Entries.Remove(entry);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, entry);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}